﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using APP.QUANLY.Common;
using RMAS.DAL;
using Sys;

namespace RMAS
{
    public partial class frmLogin : Form
    {
        public bool OK = false;
        private RegistrySetting setting;
        public frmLogin()
        {
            InitializeComponent();
        }
        private void cmdClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void cmdLogin_Click(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            this.lblMsg.Visible = false;
            if ((this.txtUserName.Text.Trim() == "") || (this.txtPassWord.Text.Trim() == ""))
            {
                this.lblMsg.Visible = true;
                this.lblMsg.Text = "        Bạn phải nhập vào đầy đủ thông tin!";
                if (this.txtUserName.Text == "")
                {
                    this.txtUserName.Focus();
                }
                else
                {
                    this.txtPassWord.Focus();
                }
            }
            else
            {
                bool flag = false;
                try
                {
                    DM_USERDao execute = new DM_USERDao();
                    DM_USER user = execute.SelectGetLogin(txtUserName.Text, txtPassWord.Text.Trim());
                    if (user == null)
                    {
                        flag = false;
                        MessageBox.Show("Người dùng này không tồn tại trong hệ thống!");
                        return;
                    }
                    else
                    {
                        flag = true;
                        MessageBox.Show("Đăng nhập thành công!");
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
                if (!flag)
                {
                    this.lblMsg.Visible = true;
                    this.lblMsg.Text = "        Thông tin đăng nhập không hợp lệ!";
                }
                else
                {
                    this.OK = true;
                    base.Close();
                }
            }
        }
        private void frmLogin1_Load(object sender, EventArgs e)
        {
            setting = new RegistrySetting();
            string SerialKey = "";
            if (this.LoadFirstRun(out SerialKey))
            {
                bool RunFirstTime = true;
                if (SerialKey != "")
                {
                    frmDM_Branch bra = new frmDM_Branch(SerialKey, RunFirstTime);
                    if (bra.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                        base.Close();
                }
                else
                {
                    MessageBox.Show("Không thể tạo khoá cho hệ thống! \r\n Vui lòng liên hệ nhà cung cấp.");
                    Environment.Exit(0);
                }
            }
            else
            {
                SoftLicence SL = new SoftLicence();
                RunTypes type = setting.CheckDemo(SerialKey, out SL);
                if (type == RunTypes.UnKnown)
                {
                    MessageBox.Show("Vui lòng liên hệ nhà cung cấp theo thông tin sau!!!");
                    frmHeplerInfo fhelper1 = new frmHeplerInfo();
                    fhelper1.ShowDialog();
                    Environment.Exit(0);
                }

                getDM_BRANCH();
                if (type == RunTypes.Trial || type == RunTypes.Expired)
                {
                    frmRegisterSoftWare freg = new frmRegisterSoftWare(SL, type);
                    if (freg.ShowDialog() == DialogResult.OK)
                    {
                        if (type == RunTypes.Expired)
                            Environment.Exit(0);
                        return;
                    }
                }
                else
                {
                    return;
                }
               
            }
            //OK = false;
            //OK = CheckingCompanyInfo();
            //if (!OK)
            //{
            //    frmDM_Branch bra = new frmDM_Branch();
            //    if (bra.ShowDialog() != DialogResult.OK)
            //    {
            //        Application.Exit();
            //    }
            //}
            //else
            //{
            //    this.txtUserName.Focus();
            //}
        }
        private void getDM_BRANCH()
        {
            DM_BRANCH obj = null;
            try
            {
                DM_BRANCH_Provider _prov = new DM_BRANCH_Provider();
                string CPUID = Utils.GetCPUID();
                DM_BRANCHList list = _prov.GetAll();
                if (list != null && list.Count > 0)
                {
                    obj = list.Find(CPUID);
                    if (obj != null)
                    {
                        StaticVar.CPUID = obj.ID;
                        StaticVar.CurrBranch = obj;
                    }
                }
            }
            catch { }
        }
        private void frmLogin1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }
        private void txtUserName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.txtPassWord.Focus();
            }
        }
        private void txtUserName_Enter(object sender, EventArgs e)
        {
            this.txtUserName.BackColor = Color.Aqua;
        }
        private void txtUserName_Leave(object sender, EventArgs e)
        {
            this.txtUserName.BackColor = Color.White;
        }
        private void txtPassWord_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar == '\r') && (this.txtPassWord.Text != ""))
            {
                if ((this.txtUserName.Text != "") && (this.txtPassWord.Text != ""))
                {
                    this.cmdLogin.PerformClick();
                }
                else if (this.txtUserName.Text == "")
                {
                    this.txtUserName.Focus();
                }
            }
        }
        private void txtPassWord_Enter(object sender, EventArgs e)
        {
            this.txtPassWord.BackColor = Color.Aqua;
        }
        private void txtPassWord_Leave(object sender, EventArgs e)
        {
            this.txtPassWord.BackColor = Color.White;
        }

        private bool LoadFirstRun(out string SerialKey)
        {
            SerialKey = "";
            //if (!setting.RunFirstTime())
            SoftLicence SL = setting.RunFirst();
            if (SL == null)
            {
                //setting.SetRegValueLicence30RunFirst(out SerialKey, out DayBegin, out MonthBegin, out YearBegin);
                SL = setting.RunContinue();
                SerialKey = SL.SerialKey;
                return true;
            }
            else
            {
                SerialKey = SL.SerialKey;
                return false;
            }
        }
        private void LoadRunTrial()
        {
        }
        private bool CheckingCompanyInfo()
        {
            OK = false;
            string CPUID = Utils.GetCPUID();
            DM_BRANCH_Provider _prov =new DM_BRANCH_Provider();
            DM_BRANCHList list = _prov.GetAll();
            if (list != null && list.Count > 0)
            {
                DM_BRANCH obj = list.Find(CPUID);
                if (obj != null)
                {
                    StaticVar.CurrBranch = obj;
                    StaticVar.CPUID = obj.ID;
                    OK = true;
                }
            }
            return OK;
        }
    }
}
